%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plot_policy_IRF.m
% plot the policy functions of the baseline model and the impulse responses of the baseline model and the real only model
%
% Ivan Alfaro, Nick Bloom and Xiaoji Lin 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Figure: Policy functions

clearvars; clc;

load ks_het_firms_baseline.mat

figure 

ymin = -2.01;
ymax = 1.01;
xmin = 0;
xmax = 1;
font = 16;
ls   = 2;
index = find((k0<=380)& (k0>0));
imA   = ceil(Anum/2);
imz   = ceil(znum/2);
imK   = ceil(Knum/2)+3;
imn   = ceil(nnum/2);

Imax = abs(max(max(max(max(max(max(max(I(imA,1,1,imK ,imz,1,index,imn)))))))));
IK = I./Imax;

subplot(2,2,1)
plot(k0(index)/k0(index(end)),squeeze(IK(imA,1,1,imK ,imz,1,index,imn)),'b-.',...
    k0(index)/k0(index(end)),squeeze(IK(imA,1,2,imK,imz,1,index,imn)),'r','LineWidth',ls)
% plot investment for low and high sigma states
legend( 'Low \sigma, low finC', 'Low \sigma, high finC', 'Location','NE'); 
legend('boxoff')
orient landscape
xlabel('K','FontSize',font)
ylabel('Investment','FontSize',font)
set(gca, 'Fontsize', font);
axis([xmin xmax ymin ymax]); 
grid on;     

subplot(2,2,2)

plot(k0(index)/k0(index(end)),squeeze(IK(imA,1,1,imK ,imz,1,index,imn)),'b-.',...
    k0(index)/k0(index(end)),squeeze(IK(imA,2,1,imK,imz,2,index,imn)),'r','LineWidth',ls)
legend( 'Low \sigma,  low finC', 'High \sigma, low finC', 'Location','NE'); 
%title('Output','FontSize',fonts); 
legend('boxoff')
orient landscape
xlabel('K','FontSize',font)
%ylabel('Investment','FontSize',font)
set(gca, 'Fontsize', font);
axis([xmin xmax ymin ymax]); 
grid on;     

subplot(2,2,3)
plot(k0(index)/k0(index(end)),squeeze(IK(imA,1,1,imK ,imz,1,index,imn)),'b-.',...
    k0(index)/k0(index(end)),squeeze(IK(imA,2,2,imK,imz,2,index,imn)),'r','LineWidth',ls)
legend( 'Low \sigma,  low finC', 'High \sigma, high finC', 'Location','NE'); 
legend('boxoff')
orient landscape
xlabel('K','FontSize',font)
ylabel('Investment','FontSize',font)
set(gca, 'Fontsize', font);
axis([xmin xmax ymin ymax]); 
grid on;     

orient landscape
   

%% Figre: impulse response functions of the baseline model and the real only model

clearvars; clc

% Real only model
load IRF_Real_Only.mat  

figure

ls      = 1.25;
ms      = 8;
TC      = 7;
Time    = (-1:1:TC-1);
xmin    = -1;
xmax    = TC-1;

Lengthplot = TC;
fsize      = 16;

subplot(2,3,1)
plot(Time, [0;(mean(YsimIRF(shockperIRF-1:shockperIRF+Lengthplot-2,:),2)./mean(YsimIRF(shockperIRF-1,:))-1)*100],...
    'k-x','LineWidth',ls,'MarkerSize',ms);
hold on

subplot(2,3,2)
plot(Time, [0;(mean(IsimIRF(shockperIRF-1:shockperIRF+Lengthplot-2,:),2)./mean(IsimIRF(shockperIRF-1,:))-1)*100],...
    'k-x','LineWidth',ls,'MarkerSize',ms);
hold on

subplot(2,3,3)
plot(Time, [0;(mean(CsimIRF(shockperIRF-1:shockperIRF+Lengthplot-2,:),2)./mean(CsimIRF(shockperIRF-1,:))-1)*100],...
    'k-x','LineWidth',ls,'MarkerSize',ms);
hold on

subplot(2,3,4)
plot(Time, zeros(length(Time),1),'k-x','LineWidth',ls,'MarkerSize',ms);
hold on

subplot(2,3,5) 
plot(Time, [0;(mean(DivsimIRF(shockperIRF-1:shockperIRF+Lengthplot-2,:),2)./mean(DivsimIRF(shockperIRF-1,:))-1)*100],...
    'k-x','LineWidth',ls,'MarkerSize',ms);
hold on


subplot(2,3,6) 
plot(Time, [0;(mean(TFPsimIRF(shockperIRF-1:shockperIRF+Lengthplot-2,:),2)./mean(TFPsimIRF(shockperIRF-1,:))-1)*100],...
    'k-x','LineWidth',ls,'MarkerSize',ms);
hold on

% Baseline model with real and financial adjustment costs
load IRF_Sigma_Baseline.mat

subplot(2,3,1)
plot(Time, [0;(mean(YsimIRF(shockperIRF-1:shockperIRF+Lengthplot-2,:),2)./mean(YsimIRF(shockperIRF-1,:))-1)*100],...
    'r-v','LineWidth',ls,'MarkerSize',ms);
yticks([-6 -4  -2  0 2 4])
title('Output','FontSize',fsize);set(gca, 'Fontsize', fsize);grid on; axis([xmin xmax -5 5])
hold on

subplot(2,3,2)
plot(Time, [0;(mean(IsimIRF(shockperIRF-1:shockperIRF+Lengthplot-2,:),2)./mean(IsimIRF(shockperIRF-1,:))-1)*100],...
    'r-v','LineWidth',ls,'MarkerSize',ms);
title('Investment','FontSize',fsize);set(gca, 'Fontsize', fsize);grid on; axis([xmin xmax -15 10])
hold on

subplot(2,3,3)
plot(Time, [0;(mean(CsimIRF(shockperIRF-1:shockperIRF+Lengthplot-2,:),2)./mean(CsimIRF(shockperIRF-1,:))-1)*100],...
    'r-v','LineWidth',ls,'MarkerSize',ms);
yticks([-4  -2   0 2 4])
title('Consumption','FontSize',fsize);set(gca, 'Fontsize', fsize);grid on; axis([xmin xmax -2 2])
hold on

subplot(2,3,4)
plot(Time, [0;(mean(NsimIRF(shockperIRF-1:shockperIRF+Lengthplot-2,:),2)./mean(NsimIRF(shockperIRF-1,:))-1)*100],...
    'r-v','LineWidth',ls,'MarkerSize',ms);
title('Cash','FontSize',fsize);set(gca, 'Fontsize', fsize);grid on; axis([xmin xmax -50 60])
hold on

subplot(2,3,5) 
plot(Time, [0;(mean(DivsimIRF(shockperIRF-1:shockperIRF+Lengthplot-2,:),2)./mean(DivsimIRF(shockperIRF-1,:))-1)*100],...
    'r-v','LineWidth',ls,'MarkerSize',ms);
title('Dividend','FontSize',fsize);set(gca, 'Fontsize', fsize);grid on; axis([xmin xmax -20 20])
hold on

subplot(2,3,6) 
plot(Time, [0;(mean(TFPsimIRF(shockperIRF-1:shockperIRF+Lengthplot-2,:),2)./mean(TFPsimIRF(shockperIRF-1,:))-1)*100],...
    'r-v','LineWidth',ls,'MarkerSize',ms);
yticks([-8 -6 -4  -2 0 2  4])
title('TFP','FontSize',fsize);set(gca, 'Fontsize', fsize);grid on; axis([xmin xmax -8 5])
hold on

legend('Real adj costs', 'Real+Financial adj costs','Location','Best'); legend('boxoff');
set(legend,'FontSize',fsize);

orient landscape


